package cn.mrcode.fd.plugin.security.tag;

import org.apache.shiro.web.tags.SecureTag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.jsp.JspException;

/**
 * 是否是博主本人
 *
 * @author : zhuqiang
 * @version : V1.0
 * @date : 2016/1/2 15:50
 */
public class IsMasterUserTag extends SecureTag {
    private static final Logger log = LoggerFactory.getLogger(IsMasterUserTag.class);
    private String masterAccount; //博主的账户
    @Override
    public int onDoStartTag() throws JspException {
        Object principal = getSubject().getPrincipal();
        if (getSubject() != null && principal != null) {
            if (log.isTraceEnabled()) {
                log.trace("Subject has known identity (aka 'principal').  " +
                        "Tag body will be evaluated.");
            }
            String userAccount = (String)principal; //当前登录的账户
            if(userAccount.equals(masterAccount)){
                return EVAL_BODY_INCLUDE;
            }else{
                return SKIP_BODY;
            }
        } else {
            if (log.isTraceEnabled()) {
                log.trace("Subject does not exist or have a known identity (aka 'principal').  " +
                        "Tag body will not be evaluated.");
            }
            return SKIP_BODY;
        }
    }

    public String getMasterAccount() {
        return masterAccount;
    }

    public void setMasterAccount(String masterAccount) {
        this.masterAccount = masterAccount;
    }
}
